(function($){
    checkIfLogin(); 
         

    //初始化商家信息
    initShopInfo();
    //初始化订单
    initCart();
    //initOrder();
    //初始化地址
    //initAddress();
    //初始化支付方式
    //initPayMethod();
    //初始化积分
    //initJifen();
    //初始化送达时间
    //initArrivedTime();
    //计算实付价格
    //initPayPrice();
    ////保存地址
    //$('.save-btn').click(function() {
    //    saveAddress();
    //});
    ////提交订单
    //$('.commit-btn').click(function(event) {
    //
    //    var paymethod=my_GetValue("paymethod");
    //
    //    var price=$('.checkout-bottom-price').text();   //总价
    //    var orderArrivedTime=$('.select-arrived-time').val(); //到货时间
    //    var orderRemark=$('.liuyan-txt').val(); //留言
    //    //存必要信息
    //    my_SaveValue("price",price);
    //    my_SaveValue("orderArrivedTime",orderArrivedTime);
    //    my_SaveValue("orderRemark",orderRemark);
    //
    //    //货到付款，则当页提交
    //    if(paymethod==2){
    //        commitOrder();
    //        return;
    //    }
    //    //否则跳转到支付页
    //    $(this).attr("href","/pay.php");
    //});
    //
    ////支付方式切换
    //$(".checkout-pay").click(function() {
    //    if($(this).hasClass('disabled')){
    //        $(this).removeClass('disabled');
    //        $(this).addClass('active');
    //        $(this).siblings().removeClass('active');
    //        $(this).siblings().addClass('disabled');
    //    }
    //    //存cookie
    //    if($('.weixin-pay').parent().hasClass('active')){
    //        my_SaveValue("paymethod","0");
    //    }else if($('.alipay-pay').parent().hasClass('active')){
    //        my_SaveValue("paymethod","1");
    //    }else if($('.no-pay').parent().hasClass('active')){
    //        my_SaveValue("paymethod","2");
    //    }
    //});
})(jQuery);

function checkIfLogin(){
    var userId=$.cookie('userId');
    //TODO:My
    //if(!userId){
    //    location.href="/place.html";//跳到首页
    //}
}

//初始化商家信息
function initShopInfo(){
    var shopName=shop_GetValue("shopName");
    if(shopName){ 
        $(".info-place").html(shopName);
    }
}

//检索所有对象
function queryAll(shopId){
    var arrTxt=$.cookie('cart'+shopId);
    if(arrTxt){
        return eval('('+arrTxt+')');
    }else{
        return;
    }
};


function  initCart() {

    var shopId=$.cookie('shopId');
    if(shopId){
        var arrObj=queryAll(shopId);
        //console.log("arrObj");
        console.log(arrObj);

        if (shopId) {
            tableNum = $.cookie('table' + shopId);
        }

        $(".numberTable").html("第 "+tableNum+" 桌");

        if(arrObj) {
            var sumCount = 0;
            var sumPrice = 0;
            for (var i = 0; i < arrObj.length; i++) {
                var itemId = arrObj[i].itemId;
                var name = hexToString(arrObj[i].name);
                var count = arrObj[i].count;
                var price = arrObj[i].price;
                var htmlTxt="<li class='checkout-tablerow'>"
                    +"<div class='cell itemname'>"+name+"</div>"
                    +"<div class='cell itemquantity'>"+count+"</div>"
                    +"<div class='cell itemtotal'>￥"+price+"</div></li>";
                $('.checkout-body').append(htmlTxt);

                sumCount += count;
                sumPrice += price*count;//单价*个数
            }
            var bottomText="<li class='checkout-tablerow'>"
                +"<div class='cell itemname'>"+"合计"+"</div>"
                +"<div class='cell itemquantity'>"+sumCount+"</div>"
                +"<div class='cell itemtotal'>￥"+sumPrice+"</div></li>";
            $('.checkout-body').append(bottomText);

            console.log("界面加载出来 了不是>>");
            //删除cookie中的菜单数据
            //清空购物车
            removeAllItem(shopId);
        }
    } else  {
        console.log("没有数据");
        alert("当前没有下单");
    }

}


//初始化购物车
function initOrder(){ 
    var shopId=shop_GetValue("shopId");
    //initCart();



    console.log("aaaaaaaaaaaaaaa>>>"+shopId);
    if(shopId){
        console.log("bbbbbbbbbbbbbb");
        var arrObj=queryAll(shopId);
        console.log(arrObj);
        if(arrObj&&arrObj.length>0){
            var price3=0;
            var count2=0;
            var htmlTxt="<li class='checkout-tablerow'>"
                +"<div class='cell itemname'>$name</div>"
                +"<div class='cell itemquantity'>$count</div>"
                +"<div class='cell itemtotal'>￥$price</div></li>";  
            for(var i=0;i<arrObj.length;i++){
                var itemId=arrObj[i].itemId;
                var name=hexToString(arrObj[i].name);
                var count=arrObj[i].count;
                var price=arrObj[i].price;
                var price2=price*count;//单价*个数
                price3+=price2;
                count2+=count;

                var newTxt=htmlTxt.replace("$name",name).replace("$count",count).replace("$price",price2); 

                $('.checkout-body').append(newTxt); 
            }
            //合计
           var newTxt=htmlTxt.replace("$name","合计").replace("$count",count2).replace("$price",price3);  
                $('.checkout-body').append(newTxt); 
            
        }else{
            console.log("cccccccccccccccc");
            //TODO:
            //window.location.href="/shop/"+shopId;
        }
        
    }
}





//计算实付价格
function initPayPrice(){
    //var username=my_GetValue("userId");
    var shopId=shop_GetValue("shopId");
    if(shopId){  
        var arrObj=selectAll(shopId);
        if(arrObj&&arrObj.length>0){ 
            var itemsTxt=JSON.stringify(arrObj);
            var postUrl="/ajax/getPayPrice.php";  
            $.post(postUrl,  
                {
                    username:username,
                    shopId:shopId,
                    itemsTxt:itemsTxt},
                function(data,status,xhr) {   
                   if(status=="success"){   
                        $res= $.parseJSON(data); 
                        if($res.code=="0"){  
                            //实付
                            $(".checkout-bottom-price").html($res.payPrice);
                        }else if($res.code=="1"){
                           alert("获取实付价格失败,请刷新页面");
                        } 
                   }else{
                        alert("服务器异常,请刷新页面");
                   }
               }); 
        }
    } 
}

function saveUserInfo(pn,name,place,addressDetail){
    $username=$.cookie('userId');
    if(!$username){
        alert("用户未登录");
        return;
    }
    var postUrl="/ajax/saveUserInfo.php";  
    $.post(postUrl,  
        {
            username:$username,
            pn:pn,
            name:name,
            place:place,
            addressDetail:addressDetail
        },
        function(data,status,xhr) {   
           if(status=="success"){   
                $res= $.parseJSON(data); 
                if($res.code=="0"){ 
                    my_SaveValue("pn",pn);
                    my_SaveValue("name",name);
                    my_SaveValue("place",place);
                    my_SaveValue("address-detail",addressDetail); 

                }else if($res.code=="1"){
                    alert("保存失败");
                } 
           }else{
                alert("服务器异常,请重试");
           }
       }); 
}

//提交订单
function commitOrder(){ 

    var postUrl="/ajax/commitOrder.php";
    var username=$.cookie('userId'); 
    var shopId=shop_GetValue("shopId");
    var shopName=shop_GetValue("shopName");
    var shopPhone=shop_GetValue("shopPhone");
    var price=my_GetValue("price"); //价格

    var pn=my_GetValue('pn');
    var name=my_GetValue('name');
    var place=my_GetValue('place');
    var block=my_GetValue('block');
    var floor=my_GetValue('floor');
    var paymethod=my_GetValue('paymethod');

    var orderAddress=getAddress(place,block,floor);
    var orderArrivedTime=my_GetValue("orderArrivedTime");
    var orderRemark=my_GetValue("orderRemark"); 


    if(shopId){  
        var arrObj=selectAll(shopId);
        if(arrObj&&arrObj.length>0){ 
            var itemsTxt=JSON.stringify(arrObj); 
            $.post(postUrl,  
            {
                username:username,
                items:itemsTxt,
                shopId:shopId,
                shopName:shopName,
                shopPhone:shopPhone,
                price:price,
                orderAddress:orderAddress,
                paymethod:paymethod,
                name:name,
                pn:pn,
                orderArrivedTime:orderArrivedTime,
                orderRemark:orderRemark
            },
            function(data,status,xhr) {    
                console.log("Order===data："+data);
               if(status=="success"){  
                    $res= $.parseJSON(data); 
                    if($res.code=="0"){   
                         console.log("Order===提交方式："+paymethod);
                         //清空购物车
                         removeAllItem(shopId);
                          if(paymethod==0){ //微信支付   
                             //window.location.href="/weixinPay.php";//pay.php页才能执行
                         }else if(paymethod==1){ //支付宝支付 
                             //window.location.href="/aliPay.php";  //pay.php页才能执行
                         }else if(paymethod==2){ //餐到付款
                            //window.location.href="/account/Order";
                         }
                    }else if($res.code=="1"){
                        showAlert($res.msg); 
                    }
               }else{
                    showAlert("服务器异常");  
               }
           }); 
        }else{ 
            showAlert("已提交过该订单","/account/Order");
        }
    }
}
